﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace LiveMuse
{
    public class MouseOperation
    {
        [DllImport("User32")]
        public extern static void mouse_event(int flags, int x, int y, int data, IntPtr extrainfo);
        /// <summary>
        /// 鼠标按键的ASCII码。
        /// </summary>
        public enum MouseEventFlags       
        {
            Move = 0x0001,
            LeftDown = 0x0002,
            LeftUp = 0x0004,
            RightDown = 0x0008,
            RightUp = 0x0010,
            MiddleDown = 0x0020,
            MiddleUp = 0x0040,
            Wheel = 0x0800,
            Absolute = 0x8000
        }
        ///// <summary>
        ///// 鼠标单击操作
        ///// </summary>
        ///// <param name="point"></param>
        //public void click(Point point) 
        //{
        //    CursorLocation location=new CursorLocation(point);
        //    location.SetCursorLocation();
        //    try
        //    {
        //        mouse_event((int)(MouseEventFlags.LeftDown|MouseEventFlags.LeftUp),0,0,0,IntPtr.Zero);
        //    }
        //    catch (Exception ex)
        //    {
        //        MessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        //        return;
        //    }
        //}
        ///// <summary>
        ///// 鼠标双击操作。
        ///// </summary>
        //public void doubleclick(Point point) 
        //{
        //    CursorLocation location = new CursorLocation(point);
        //    location.SetCursorLocation();
        //    try
        //    {
        //        mouse_event((int)(MouseEventFlags.LeftDown | MouseEventFlags.LeftUp), 0, 0, 0, IntPtr.Zero);
        //        mouse_event((int)(MouseEventFlags.LeftDown | MouseEventFlags.LeftUp), 0, 0, 0, IntPtr.Zero);
        //    }
        //    catch (Exception ex)
        //    {
        //        MessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        //        return;
        //    }
        //}
        ///// <summary>
        ///// 鼠标右击操作。
        ///// </summary>
        //public void rightclick(Point point)
        //{
        //    CursorLocation location = new CursorLocation(point);
        //    location.SetCursorLocation();
        //    try
        //    {
        //        mouse_event((int)(MouseEventFlags.RightDown | MouseEventFlags.RightUp), 0, 0, 0, IntPtr.Zero);
        //    }
        //    catch (Exception ex)
        //    {
        //        MessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        //        return;
        //    }
        //}

        public void move(int x, int y)
        {
            //CursorLocation location = new CursorLocation(point);
            //location.SetCursorLocation();
            try
            {
                mouse_event((int)(MouseEventFlags.Move), x, y, 0, IntPtr.Zero);
            }
            catch (Exception ex)
            {
                Console.Write("Failed to keep mouse live: {0}", ex);
                return;
            }
        }
    }
}
